﻿using System.Web.Services;
using System.Collections.Specialized;
using System.Collections.Generic;
using TripLOA.WebService.Serializable;
using TripLOA.WebService.Wrappers;

namespace TripLOA.WebService.Rest
{
    [WebService(Namespace = "http://triploa.cli.di.unipi.it/service/rest/", Name = "RestTrip")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class RestMessage : EndPoint
    {
        private MessageCore core;
        
        #region Costructors

        public RestMessage()
        {
            core = MessageCore.Instance();
        }

        #endregion

        #region Message Methods

        public string SendMessage(NameValueCollection _REQUEST, string format)
        {
            List<WSParameter> parameters = new List<WSParameter>();
            parameters.Add(new WSParameter() { type = typeof(bool), value = _REQUEST["priority"] });

            if (!WsCore.ValidateParameters(parameters))
            {
                //Request contains invalid parameters
                WSError wse = new WSError();
                wse.ErrorType = ErrorType.InvalidParameter;
                wse.Message = "Request contains invalid parameters";
                return Serialization.serialize(wse, typeof(WSError), format);
            }
            
            //Build a message
            Message msg = new Message()
            {
                Title = _REQUEST["title"],
                Text = _REQUEST["text"],
                Priority = bool.Parse(_REQUEST["priority"])
            };

            //TODO use a comma separated list (to split)?
            List<string> receivers = new List<string>();
            System.Text.StringBuilder receiver = new System.Text.StringBuilder();
            foreach (char c in _REQUEST["receivers"])
            {
                if (c != ',')
                    receiver.Append(c);
                else
                {
                    receivers.Add(receiver.ToString());
                    receiver = new System.Text.StringBuilder();
                }
            }
            receivers.Add(receiver.ToString());
            WSBool wsb = core.SendMessage(_REQUEST["auth_token"], msg, receivers);
            return Serialization.serialize(wsb, typeof(WSBool), format);
        }

        public string MarkMessageAsRead(NameValueCollection _REQUEST, string format)
        {
            List<WSParameter> parameters = new List<WSParameter>();
            parameters.Add(new WSParameter() { type = typeof(int), value = _REQUEST["message_id"] });

            if (!WsCore.ValidateParameters(parameters))
            {
                //Request contains invalid parameters
                WSError wse = new WSError();
                wse.ErrorType = ErrorType.InvalidParameter;
                wse.Message = "Request contains invalid parameters";
                return Serialization.serialize(wse, typeof(WSError), format);
            }

            WSBool wsb = core.MarkMessageAsRead(_REQUEST["auth_token"], int.Parse(_REQUEST["message_id"]));
            return Serialization.serialize(wsb, typeof(WSBool), format);
        }

        public string GetUnreadMessages(NameValueCollection _REQUEST, string format)
        {
            WSMessage wsm = core.GetUnreadMessages(_REQUEST["auth_token"]);
            return Serialization.serialize(wsm, typeof(WSMessage), format);
        }

        public string GetReceivedMessages(NameValueCollection _REQUEST, string format)
        {
            List<WSParameter> parameters = new List<WSParameter>();
            parameters.Add(new WSParameter() { type = typeof(int), value = _REQUEST["start"] });
            parameters.Add(new WSParameter() { type = typeof(int), value = _REQUEST["max"] });

            if (!WsCore.ValidateParameters(parameters))
            {
                //Request contains invalid parameters
                WSError wse = new WSError();
                wse.ErrorType = ErrorType.InvalidParameter;
                wse.Message = "Request contains invalid parameters";
                return Serialization.serialize(wse, typeof(WSError), format);
            }

            WSMessage wsm = core.GetReceivedMessages(_REQUEST["auth_token"], int.Parse(_REQUEST["start"]), int.Parse(_REQUEST["max"]));
            return Serialization.serialize(wsm, typeof(WSMessage), format);
        }

        public string DeleteMessage(NameValueCollection _REQUEST, string format)
        {
            List<WSParameter> parameters = new List<WSParameter>();
            parameters.Add(new WSParameter() { type = typeof(int), value = _REQUEST["message_id"] });

            if (!WsCore.ValidateParameters(parameters))
            {
                //Request contains invalid parameters
                WSError wse = new WSError();
                wse.ErrorType = ErrorType.InvalidParameter;
                wse.Message = "Request contains invalid parameters";
                return Serialization.serialize(wse, typeof(WSError), format);
            }

            WSBool wsb = core.DeleteMessage(_REQUEST["auth_token"], int.Parse(_REQUEST["message_id"]));
            return Serialization.serialize(wsb, typeof(WSBool), format);
        }

        public string GetSentMessages(NameValueCollection _REQUEST, string format)
        {
            List<WSParameter> parameters = new List<WSParameter>();
            parameters.Add(new WSParameter() { type = typeof(int), value = _REQUEST["start"] });
            parameters.Add(new WSParameter() { type = typeof(int), value = _REQUEST["max"] });

            if (!WsCore.ValidateParameters(parameters))
            {
                //Request contains invalid parameters
                WSError wse = new WSError();
                wse.ErrorType = ErrorType.InvalidParameter;
                wse.Message = "Request contains invalid parameters";
                return Serialization.serialize(wse, typeof(WSError), format);
            }

            WSMessage wsm = core.GetSentMessages(_REQUEST["auth_token"], int.Parse(_REQUEST["start"]), int.Parse(_REQUEST["max"]));
            return Serialization.serialize(wsm, typeof(WSMessage), format);
        }

        #endregion
    }
}
